package com.bit.book.Controller;

import com.bit.book.enums.BookStatusEnum;
import com.bit.book.enums.ResultCodeEnum;
import com.bit.book.model.*;
import com.bit.book.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook")
    public Result addBook(BookInfo bookInfo){
        log.info("添加图书, request:{}" + bookInfo);
        // 1. 参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())){
            log.error("添加图书, 参数不合法, request:{}", bookInfo);
        }
        if(!StringUtils.hasLength(bookInfo.getAuthor())){
            return Result.fail("图书作者不能为空!");
        }
        if(bookInfo.getCount()==null){
            return Result.fail("图书数量不能为空");
        }
        if(bookInfo.getPrice()==null){
            return Result.fail("图书价格不能为空");
        }
        if(!StringUtils.hasLength(bookInfo.getPublish())){
            return Result.fail("图书出版社不能为空");
        }
        if(bookInfo.getStatus()==null){
            return Result.fail("图书状态不能为空");
        }

        // 2. 存储数据
        try{
            bookService.addBook(bookInfo);
            return Result.success("");
        }catch (Exception e){
            log.error("添加图书异常, e:" ,e);
            // 3. 返回结果
            return Result.fail("添加图书发生异常, 请联系管理员!");
        }
    }

    // 返回页数接口
    @RequestMapping("/getListByPage")
    public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
        ResponseResult<BookInfo> listByPage = bookService.getListByPage(pageRequest);
        return Result.success(listByPage);
    }

    @RequestMapping("/queryBookById")
    public Result queryBookById(Integer bookId){
        log.info("查询图书信息, bookId: "+ bookId);
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return Result.success(bookInfo);
    }

    @RequestMapping(value = "/updateBook")
    public Result updateBook(BookInfo bookInfo){
        log.info("修改图书, bookInfo: {}", bookInfo);
        try {
            // 修改图书逻辑
            bookService.updateBook(bookInfo);
            // 修改图书成功, 返回一个空字符串
            return Result.success("");
        }catch (Exception e){
            log.error("修改图书发生异常, e: ", e);  // 用于在后端查看异常日志, 不用返回给前端
            return Result.fail("修改图书失败");
        }
    }

    @RequestMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        log.info("删除图书, bookId {}", bookId);
        try {
            // 删除图书逻辑
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookService.updateBook(bookInfo);
            // 修改图书成功, 返回一个空字符串
            return Result.success("");
        }catch (Exception e){
            log.error("删除图书发生异常, e: ", e);  // 用于在后端查看异常日志, 不用返回给前端
            return Result.fail("删除图书发生异常");
        }
    }

    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids: {}", ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败{}", e);
            return false;
        }
    }


}
